<template>
    <div>

        <div class="content-title">
            <p>开通学校</p>
        </div>

        <div class="content-body">
            <div>

                <div class="content-tit">
                    学校管理
                    <div class="action right">
                        <div class="btn" @click="openUp">开通学校</div>
                    </div>
                    <div class="aa"></div>
                </div>

                <div class="content-cont">
                    <el-table :data="tableData" border :highlight-current-row="false" style="width: 100%">
                        <el-table-column prop="schoolId" label="学校ID" width="120" align="center">

                        </el-table-column>
                        <el-table-column prop="schoolName" label="学校名称" align="center">

                        </el-table-column>
                        <el-table-column prop="admPhone" label="管理员账号" align="center">

                        </el-table-column>
                        <el-table-column prop="createTimeString" label="开通时间" align="center">

                        </el-table-column>
                        <el-table-column prop="action" label="操作" align="center">
                            <template slot-scope="scope">
                                <span class="resetting" @click="openResetting(scope)">重置密码</span>
                            </template>
                        </el-table-column>
                    </el-table>
                    <el-pagination
                        layout="prev, pager, next"
                        :total="totalCount"
                        @current-change="handleSizeChange">
                    </el-pagination>
                </div>

            </div>
        </div>

        
        <el-dialog
        title="提示"
        :visible.sync="balanceDialog"
        width="30%">
            <span>请先充值钱包，在进行学校开通</span>
            <span slot="footer" class="dialog-footer">
                <el-button @click="balanceDialog = false">取 消</el-button>
                <el-button type="primary" @click="toPay">去充值</el-button>
            </span>
        </el-dialog>
        
        <el-dialog
        title="重置密码"
        :visible.sync="resettingDialog"
        :close="close"
        width="30%">
            <span>确认将该学校管理员密码重置为6个1</span>
            <span slot="footer" class="dialog-footer">
                <el-button @click="resettingDialog = false">取 消</el-button>
                <el-button type="primary" @click="resetting">确认重置</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script>
	import utils from '@/assets/js/urlConfig.js'
    export default {
        data() {
            return {
                tableData: [],
                balance: -1,
                balanceDialog: false,
                resettingDialog: false,
                resettingItem: null,
                totalCount: 0,
                pageSize: 10,
                page: 1,
            }
        },
        mounted() {

            this.accountBalance();
            this.listSchool();
        },
        methods: {
            // 获取学校列表
            listSchool() {
                var vm = this;
                this.$http({
                    method: 'POST',
                    url: utils.UrlConfig.listSchool + '?token=' + $cookieStore.getAll('home_').tokenKey,
                    data: {
                        pageNo: vm.page,
                        pageSize: 10,
                        accountId: $cookieStore.getAll("home_").accountId,
                    }
                }).then(function(res) {
                    if (res.data.code != 200) {
                        this.$message.error(res.data.msg);
                        return;
                    }
                    vm.tableData = res.data.data.result;
                    vm.totalCount = res.data.data.totalCount;
                    for (var i = 0 ; i < vm.tableData.length ; i++) {
                        var date = new Date(vm.tableData[i].createTime);
                        var year = date.getFullYear();
                        var month = date.getMonth() + 1;
                        var day = date.getDate();
                        var hours = date.getHours();
                        var minutes = date.getMinutes();
                        vm.tableData[i]['createTimeString'] = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes
                    }
                })
            },
            // 跳页
            handleSizeChange(page) {
                this.page = page;
                this.listSchool();
            },
            // 开通学校页面
            openUp(info, to) {
                var vm = this;
                console.log(vm.balance)
                if (vm.balance.balance < vm.balance.consume) {
                    vm.balanceDialog = true;
                    return;
                }
                this.$router.push({path: '/userManagement/openUp'})
            },
            // 打开弹窗
            openResetting(item) {
                this.resettingDialog = true;
                this.resettingItem = item;
            },
            // 关闭重置弹窗
            close() {
                this.resettingItem = null;
            },
            // 重置密码
            resetting() {
                var vm = this;
                vm.$http({
                    method: 'GET',
                    url: utils.UrlConfig.updatePass,
                    params: {
                        token: $cookieStore.getAll('home_').tokenKey,
                        schoolId: Number(vm.resettingItem.row.schoolId)
                    }
                }).then(function(res) {
                    if (res.data.code != 200) {
                        this.$message.error(res.data.msg);
                        return;
                    }
                    this.$message.success('重置成功');
                    vm.resettingDialog = false;
                })
            },
            // 获取余额
            accountBalance() {
                var vm = this;
                vm.$http({
                    method: 'GET',
                    url: utils.UrlConfig.accountBalance,
                    params: {
                        token: $cookieStore.getAll('home_').tokenKey,
                        accountId: $cookieStore.getAll("home_").accountId,
                    }
                }).then(function(res) {
                    console.log(res)
                    if (res.data.code != 200) {
                        this.$message.error(res.data.msg);
                        return;
                    }
                    vm.balance = res.data.data;
                })
            },
            // 去支付
            toPay() {
                this.$router.push({path: '/walletManagement/createRecharge'})
            }
        }
    }
</script>

<style scoped>
.resetting {
    color: #68a1d1;
    cursor: pointer;
}
</style>
